package com.bocai.controller;

import com.bocai.bean.RechargeInfo;
import com.bocai.service.IRechargeInfoService;
import com.bocai.utils.CodeEnum;
import com.bocai.utils.DateUtils;
import com.bocai.utils.RandomUtils;
import com.bocai.utils.ResponseData;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;

/**
 * Created by jyf on 18/4/13.
 */
@Controller
@RequestMapping("rechargeInfo")
public class RechargeInfoController {

    @Autowired
    private IRechargeInfoService rechargeInfoService;

    @ApiOperation(value = "充值钱数", notes = "充值钱数")
    @RequestMapping(value = "/saveRechargeInfo", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData findUserInfo(String phone, BigDecimal money, String paymentType) {
        RechargeInfo rechargeInfo = new RechargeInfo();
        rechargeInfo.setRechargeMoney(money);
        rechargeInfo.setCreateTime(new Date());
        String orderNum = RandomUtils.getOrderNumber();
        rechargeInfo.setRechargeNumber(orderNum);
        rechargeInfo.setPaymentType(paymentType);
        ResponseData data = new ResponseData();
        if (null == phone || phone.trim().length() < 5) {
            data.setCodeEnum(CodeEnum.CODE_90002);
        } else {
            int num = rechargeInfoService.insertSelective(rechargeInfo, phone);
            if (num == 1) {
                data.setCodeEnum(CodeEnum.SUCCESS);
            } else if (num == 2) {
                data.setCodeEnum(CodeEnum.CODE_90005);
            } else {
                data.setCodeEnum(CodeEnum.CODE_10003);
            }
        }
        return data;
    }


    @ApiOperation(value = "查询充值 提现 记录  userId 可以为空 代表全部", notes = "查询充值 提现 记录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "flag", dataType = "integer", value = "全部1 充值2 提现3", defaultValue = "1"),
    })
    @RequestMapping(value = "/findAllRechargeInfo", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData findAllRechargeInfo(Integer userId, String staTime, String endTime, Integer flag) {
        ResponseData data = new ResponseData();
        if ((null != staTime) && (null != endTime)) {
            try {
                Date staDate = DateUtils.parse(staTime + " 00:00:00");
                Date endDate = DateUtils.parse(endTime + " 23:59:59");
                if (flag > 3) {
                    data.setCodeEnum(CodeEnum.CODE_90002);
                } else {
                    data.setResults(rechargeInfoService.findAllRechargeInfo(userId, staDate, endDate, flag));
                }
            } catch (ParseException e) {
                e.printStackTrace();
                data.setCodeEnum(CodeEnum.CODE_90002);
            }
        } else if ((null == staTime || staTime.trim().equals("")) && (null == endTime || endTime.trim().equals(""))) {
            try {
                Date staDate2 = DateUtils.parse("1992-12-12 00:00:00");
                data.setResults(rechargeInfoService.findAllRechargeInfo(userId, staDate2, new Date(), flag));
            } catch (ParseException e) {
                e.printStackTrace();
                data.setCodeEnum(CodeEnum.CODE_90002);
            }
        } else {
            data.setCodeEnum(CodeEnum.CODE_90002);
        }
        return data;
    }


}
